package org.nanhill.osgi.example.web.impl;

/*
 * RIAWork.org
 * 
 * OSGI Opendoc Demo
 */
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nanhill.osgi.example.manager.AccountNotExist;
import org.nanhill.osgi.example.web.LoginService;
import org.osgi.framework.BundleContext;

/**
 * desc: 登录响应Servlet
 * 
 * @author jerry
 */
public class LoginServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	private LoginService loginService;

	public LoginServlet(BundleContext context) {
		this.loginService = new DefaultLoginService(context);
	}

	/**
	 * Web Post Method
	 * 
	 * @throws ServletException
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doGet(request, response);
	}

	/**
	 * Web GET Method
	 * 
	 * @throws ServletException
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		System.out.println(request.getParameterMap());
		ServletOutputStream output = response.getOutputStream();
		try {
			loginService.valide(request.getParameterMap());
			output.println(" login Success!");
		} catch (AccountNotExist e) {
			output.println(" login Fail!");
		}
		return;
	}
}
